class Admins::JifensController < ApplicationController
  def index
     t = Time.now.at_end_of_day.strftime("%Y-%m-%d %H:%M:%S")
    t1 = Time.mktime(Time.now.year,1,1).beginning_of_day.strftime("%Y-%m-%d %H:%M:%S")
   
    
    session[:news_begin] = params[:news_begin] ? params[:news_begin].to_date.beginning_of_day.strftime("%Y-%m-%d %H:%M:%S") : t1
    session[:news_end] = params[:news_end] ? params[:news_end].to_date.at_end_of_day.strftime("%Y-%m-%d %H:%M:%S") : t
    
    p '==================='
     p session[:news_begin] 
    p session[:news_end] 
    
    departments = Department.where("short_name is not null ")
    @tongj  =   Array.new(departments.size()) { Hash.new }
    departments.each_with_index  do |department,i|
      p i.to_s  + department.name.to_s
      @tongj[i]["department"] =  department.name.to_s
    end

    #上传
    sql = "SELECT contribution_units,count(1) as \"check\" from news WHERE 1=1 and pubtime >='"+session[:news_begin] +"' and pubtime <= '"+session[:news_end] + "' GROUP BY contribution_units "
    @result = ActiveRecord::Base.connection.execute(sql)
    #采用
    sql = "SELECT contribution_units,count(1) as  check  from news n ,news_groups  g WHERE n.\"id\" = g.news_id and g.\"check\" = 1 and g.group_id = 15  and  n.\"check\"=2 and n.pubtime >='"+session[:news_begin] +"' and n.pubtime <= '"+session[:news_end]+"'  GROUP BY contribution_units ; "
    @result2 = ActiveRecord::Base.connection.execute(sql)
    #x校外采用
    sql = "SELECT contribution_units,count(1) as  check  from news n ,news_groups  g WHERE n.\"id\" = g.news_id and g.\"check\" = 1 and g.group_id = 23 and n.pubtime >='"+session[:news_begin] +"' and n.pubtime <= '"+session[:news_end]+"'  GROUP BY contribution_units ; "
    @result3= ActiveRecord::Base.connection.execute(sql)
    #校报采用
    sql = "SELECT contribution_units,count(1) as  check  from news n ,news_groups  g WHERE n.\"id\" = g.news_id and g.\"check\" = 1 and g.group_id = 18 and n.pubtime >='"+session[:news_begin] +"' and n.pubtime <= '"+session[:news_end]+"'  GROUP BY contribution_units ; "
    @result4= ActiveRecord::Base.connection.execute(sql)
    #退稿
    sql = "SELECT contribution_units,sum(toback_count) as  \"check\" from news WHERE 1=1 and pubtime >='"+session[:news_begin] +"' and pubtime <= '"+session[:news_end]+"'  GROUP BY contribution_units "
    @result5 = ActiveRecord::Base.connection.execute(sql)

    #微信
    sql = "SELECT contribute_unit_name,count(1) as \"num\" from weixins WHERE 1=1 and \"check\" = 2 and created_at >='"+session[:news_begin] +"' and created_at <= '"+session[:news_end]+"'  GROUP BY contribute_unit_name"
    @result6 = ActiveRecord::Base.connection.execute(sql)
    #微博
    sql = "SELECT contribute_unit_name,count(1) as \"num\" from weibos WHERE 1=1 and \"check\"= 2 and created_at >='"+session[:news_begin] +"' and created_at <= '"+session[:news_end]+"'  GROUP BY contribute_unit_name"
    @result7= ActiveRecord::Base.connection.execute(sql)

    #线索
    sql = "SELECT contribution_units,count(1) as \"num\" from clues WHERE 1=1 and \"check\" = 2  and created_at >='"+session[:news_begin] +"' and created_at <= '"+session[:news_end]+"'  GROUP BY contribution_units "
    @result8 = ActiveRecord::Base.connection.execute(sql)

    @tongj.each do| tongfji|
      tongfji["shangchuan"] = 0
      tongfji["xwcy"] = 0
      tongfji["cy"] = 0
      tongfji["xbcy"] = 0
      tongfji["tg"] = 0
      tongfji["wx"] = 0
      tongfji["wb"] = 0
      tongfji["xs"] =0

      @result.each do |xw|
        if tongfji["department"] == xw["contribution_units"]
          tongfji["shangchuan"] = xw["check"].to_i
        end
      end
      @result2.each do |xw|
        if tongfji["department"] == xw["contribution_units"]
          tongfji["cy"] = xw["check"].to_i
        end
      end
      @result3.each do |xw|
        if tongfji["department"] == xw["contribution_units"]
          tongfji["xwcy"] = xw["check"].to_i
        end
      end
      @result4.each do |xw|
        if tongfji["department"] == xw["contribution_units"]
          tongfji["xbcy"] = xw["check"].to_i
        end
      end
      @result5.each do |xw|
        if tongfji["department"] == xw["contribution_units"]
          tongfji["tg"] = xw["check"].to_i
        end
      end
      @result6.each do |xw|
        if tongfji["department"] == xw["contribute_unit_name"]
          tongfji["wx"] = xw["num"].to_i
        end
      end
      @result7.each do |xw|
        if tongfji["department"] == xw["contribute_unit_name"]
          tongfji["wb"] = xw["num"].to_i
        end
      end
      @result8.each do |xw|
        if tongfji["department"] == xw["contribution_units"]
          tongfji["xs"] = xw["num"].to_i
        end
      end

      
      tongfji["tj"] =  tongfji["shangchuan"]*0.5 + tongfji["cy"]*2  +tongfji["xwcy"]*5  + tongfji["xbcy"]*2  - tongfji["tg"]*1  + tongfji["wx"]*1  + tongfji["wb"]*1 + tongfji["xs"]*1 
      
    end
    @tongj = @tongj.sort_by {|u| u["tj"]}
  end
  
  def about
    
  end
end
